/*
  Simple DirectMedia Layer
  Copyright (C) 1997-2015 Sam Lantinga <slouken@libsdl.org>

  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any damages
  arising from the use of this software.

  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.
*/

#include "../../SDL_internal.h"

#if SDL_VIDEO_DRIVER_ORBIS

#include <orbis2d.h>
/* SDL internals */
#include "../SDL_sysvideo.h"
#include "SDL_version.h"
#include "SDL_syswm.h"
#include "SDL_loadso.h"
#include "SDL_events.h"
#include "../../events/SDL_mouse_c.h"
#include "../../events/SDL_keyboard_c.h"

/* ORBIS declarations */
#include "SDL_orbisvideo.h"
#include "SDL_orbiskeyboard.h"
//#include "SDL_orbismouse_c.h"

SDL_Window *Orbis_Window;

/* unused
static SDL_bool ORBIS_initialized = SDL_FALSE;
*/
static int
ORBIS_Available(void)
{
    if(orbis2dGetStatus() == 1) {
        return 1;
    }
    else {
        return 0;
    }
}

static void
ORBIS_Destroy(SDL_VideoDevice * device)
{
/*    SDL_VideoData *phdata = (SDL_VideoData *) device->driverdata; */

    if (device->driverdata != NULL) {
        device->driverdata = NULL;
    }
}

static SDL_VideoDevice *
ORBIS_Create()
{
    SDL_VideoDevice *device;
    SDL_VideoData *phdata;
    int status;

    /* Check if ORBIS could be initialized */
    status = ORBIS_Available();
    if (status == 0) {
        /* VITA could not be used */
        return NULL;
    }

    /* Initialize SDL_VideoDevice structure */
    device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
    if (device == NULL) {
        SDL_OutOfMemory();
        return NULL;
    }

    /* Initialize internal ORBIS specific data */
    phdata = (SDL_VideoData *) SDL_calloc(1, sizeof(SDL_VideoData));
    if (phdata == NULL) {
        SDL_OutOfMemory();
        SDL_free(device);
        return NULL;
    }


    device->driverdata = phdata;

    /* Setup amount of available displays and current display */
    device->num_displays = 0;

    /* Set device free function */
    device->free = ORBIS_Destroy;

    /* Setup all functions which we can handle */
    device->VideoInit = ORBIS_VideoInit;
    device->VideoQuit = ORBIS_VideoQuit;
    device->GetDisplayModes = ORBIS_GetDisplayModes;
    device->SetDisplayMode = ORBIS_SetDisplayMode;
    device->CreateSDLWindow = ORBIS_CreateWindow;
    device->CreateSDLWindowFrom = ORBIS_CreateWindowFrom;
    device->SetWindowTitle = ORBIS_SetWindowTitle;
    device->SetWindowIcon = ORBIS_SetWindowIcon;
    device->SetWindowPosition = ORBIS_SetWindowPosition;
    device->SetWindowSize = ORBIS_SetWindowSize;
    device->ShowWindow = ORBIS_ShowWindow;
    device->HideWindow = ORBIS_HideWindow;
    device->RaiseWindow = ORBIS_RaiseWindow;
    device->MaximizeWindow = ORBIS_MaximizeWindow;
    device->MinimizeWindow = ORBIS_MinimizeWindow;
    device->RestoreWindow = ORBIS_RestoreWindow;
    device->SetWindowGrab = ORBIS_SetWindowGrab;
    device->DestroyWindow = ORBIS_DestroyWindow;
    device->GetWindowWMInfo = ORBIS_GetWindowWMInfo;
    device->HasScreenKeyboardSupport = ORBIS_HasScreenKeyboardSupport;
    device->ShowScreenKeyboard = ORBIS_ShowScreenKeyboard;
    device->HideScreenKeyboard = ORBIS_HideScreenKeyboard;
    device->IsScreenKeyboardShown = ORBIS_IsScreenKeyboardShown;

	device->PumpEvents = ORBIS_PumpEvents;

    return device;
}

VideoBootStrap ORBIS_bootstrap = {
    "ORBIS",
    "ORBIS Video Driver",
    ORBIS_Available,
    ORBIS_Create
};

/*****************************************************************************/
/* SDL Video and Display initialization/handling functions                   */
/*****************************************************************************/
int
ORBIS_VideoInit(_THIS)
{
    SDL_VideoDisplay display;
    SDL_DisplayMode current_mode;

    SDL_zero(current_mode);

    current_mode.w = 1280;
    current_mode.h = 720;

    current_mode.refresh_rate = 60;
    /* 32 bpp for default */
    current_mode.format = SDL_PIXELFORMAT_ARGB8888;

    current_mode.driverdata = NULL;

    SDL_zero(display);
    display.desktop_mode = current_mode;
    display.current_mode = current_mode;
    display.driverdata = NULL;

    SDL_AddVideoDisplay(&display);
    //ORBIS_InitTouch();
    ORBIS_InitKeyboard();
    //ORBIS_InitMouse();

    return 1;
}

void
ORBIS_VideoQuit(_THIS)
{
    //ORBIS_QuitTouch();
}

void
ORBIS_GetDisplayModes(_THIS, SDL_VideoDisplay * display)
{

}

int
ORBIS_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
{
    return 0;
}

int
ORBIS_CreateWindow(_THIS, SDL_Window * window)
{
    SDL_WindowData *wdata;

    /* Allocate window internal data */
    wdata = (SDL_WindowData *) SDL_calloc(1, sizeof(SDL_WindowData));
    if (wdata == NULL) {
        return SDL_OutOfMemory();
    }

    /* Setup driver data for this window */
    window->driverdata = wdata;

    // ORBIS can only have one window
    if (Orbis_Window != NULL)
    {
        // Replace this with something else
        return SDL_OutOfMemory();
    }

    Orbis_Window = window;

    // fix input, we need to find a better way
    SDL_SetKeyboardFocus(window);

    /* Window has been successfully created */
    return 0;
}

int
ORBIS_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
{
    return -1;
}

void
ORBIS_SetWindowTitle(_THIS, SDL_Window * window)
{
}
void
ORBIS_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
{
}
void
ORBIS_SetWindowPosition(_THIS, SDL_Window * window)
{
}
void
ORBIS_SetWindowSize(_THIS, SDL_Window * window)
{
}
void
ORBIS_ShowWindow(_THIS, SDL_Window * window)
{
}
void
ORBIS_HideWindow(_THIS, SDL_Window * window)
{
}
void
ORBIS_RaiseWindow(_THIS, SDL_Window * window)
{
}
void
ORBIS_MaximizeWindow(_THIS, SDL_Window * window)
{
}
void
ORBIS_MinimizeWindow(_THIS, SDL_Window * window)
{
}
void
ORBIS_RestoreWindow(_THIS, SDL_Window * window)
{
}
void
ORBIS_SetWindowGrab(_THIS, SDL_Window * window, SDL_bool grabbed)
{

}
void
ORBIS_DestroyWindow(_THIS, SDL_Window * window)
{
}

/*****************************************************************************/
/* SDL Window Manager function                                               */
/*****************************************************************************/
SDL_bool
ORBIS_GetWindowWMInfo(_THIS, SDL_Window * window, struct SDL_SysWMinfo *info)
{
    if (info->version.major <= SDL_MAJOR_VERSION) {
        return SDL_TRUE;
    } else {
        SDL_SetError("application not compiled with SDL %d.%d\n",
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
        return SDL_FALSE;
    }

    /* Failed to get window manager information */
    return SDL_FALSE;
}


/* TO Write Me */
SDL_bool ORBIS_HasScreenKeyboardSupport(_THIS)
{
    return SDL_FALSE;
}
void ORBIS_ShowScreenKeyboard(_THIS, SDL_Window *window)
{
}
void ORBIS_HideScreenKeyboard(_THIS, SDL_Window *window)
{
}
SDL_bool ORBIS_IsScreenKeyboardShown(_THIS, SDL_Window *window)
{
    return SDL_FALSE;
}

void ORBIS_PumpEvents(_THIS)
{
    //ORBIS_PollTouch();
    ORBIS_PollKeyboard();
	//ORBIS_PollMouse();
}

#endif /* SDL_VIDEO_DRIVER_ORBIS */

/* vi: set ts=4 sw=4 expandtab: */
